package com.letoken.platform.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminMallService;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.constant.ConstantsValue;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.dao.AdminCardConfigMapper;
import com.letoken.platform.repo.admin.po.AdminCardConfig;
import com.letoken.platform.repo.admin.req.mall.*;
import com.letoken.platform.repo.admin.res.mall.*;
import com.letoken.platform.repo.admin.util.CollectionUtils;
import com.letoken.platform.repo.app.dao.AppUserAssetsHistoryMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueRecordMapper;
import com.letoken.platform.repo.app.dao.MallCirculationHistoriesMapper;
import com.letoken.platform.repo.app.dao.TokenMapper;
import com.letoken.platform.repo.app.po.Token;
import com.letoken.platform.repo.ecommerce.dao.EcommerceProductCategoryMapper;
import com.letoken.platform.repo.ecommerce.dao.EcommerceProductMapper;
import com.letoken.platform.repo.ecommerce.po.EcommerceProduct;
import com.letoken.platform.repo.ecommerce.po.EcommerceProductCategory;
import com.letoken.platform.repo.mall.dao.*;
import com.letoken.platform.repo.mall.po.*;
import com.letoken.platform.repo.utlis.PageDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mall")
@Api(tags = "mall相关接口")
public class AdminMallController {
    @Autowired
    AppMallAuditMapper appMallAuditMapper;

    @Autowired
    private AdminMallService adminMallService;

    @Autowired
    private AppMallBannerMapper appMallBannerMapper;
    @Autowired
    private AppMallMenuConfigMapper appMallMenuConfigMapper;
    @Autowired
    AppUserIssueRecordMapper appUserIssueRecordMapper;
    @Autowired
    AppMallInfoMapper appMallInfoMapper;
    @Autowired
    AdminMallIssueConfigMapper adminMallIssueConfigMapper;
    @Autowired
    AdminMallConfigMapper adminMallConfigMapper;
    @Autowired
    AdminCardConfigMapper adminCardConfigMapper;

    @Autowired
    AppMallCardInfoMapper appMallCardInfoMapper;
    @Autowired
    AdminMallLevelConfigMapper adminMallLevelConfigMapper;

    @Resource
    AppUserAssetsHistoryMapper appUserAssetsHistoryMapper;

    @Resource
    private MallCategoryMapper categoryMapper;

    @Resource
    private MallIssueRecordsMapper issueRecordsMapper;

    @Resource
    private MallCirculationHistoriesMapper circulationHistoriesMapper;

    @Resource
    private EcommerceProductCategoryMapper ecommerceProductCategoryMapper;
    @Autowired
    TokenMapper tokenMapper;

    @Resource
    EcommerceProductMapper ecommerceProductMapper;


    @Permission(uri = "/mall/category/List",qx = "/mall/mallCategory")
    @Trim
    @PostMapping("/category/List")
    @ApiOperation(value = "商城分类列表  chapterParent=0表示一级，大于0为一级ID，表示找对应的二级列表", notes = "商城分类列表")
    public ApiResponse<PageData<MallStoreCategoryListRes>> categoryList(@RequestBody @Validated MallStoreCategoryListReq req) {
        Page<MallStoreCategoryListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<MallStoreCategoryListRes> res = appMallMenuConfigMapper.getCategoryRecord(page,req);
        return ApiResponse.create(PageDataUtil.getPageData(res));
    }



    @Permission(uri = "/mall/category/saveOrUpdate",qx = "/mall/category/saveOrUpdate")
    @Trim
    @PostMapping("/category/saveOrUpdate")
    @ApiOperation(value = "商城分类增加或修改", notes = "商城分类增加或修改")
    public ApiResponse<Integer> saveOrUpdate(@Validated @RequestBody MallStoreCategorySaveOrUpdateReq req) {
        if (ObjectUtils.isNotEmpty(req.getChapterParent()) && req.getChapterParent() > ConstantsValue.I_0){
            AppMallMenuConfig parent = appMallMenuConfigMapper.selectById(req.getChapterParent());
            if (ObjectUtils.isEmpty(parent))
                throw new ApiServiceException("上级分类不存在");
            if (!parent.getState().equals(ConstantsValue.I_0))
                throw new ApiServiceException("上级不可用");
            if (ObjectUtils.isNotEmpty(parent.getChapterParent()) && !parent.getChapterParent().equals(ConstantsValue.I_0))
                throw new ApiServiceException("只允许存在二级分类，请选择一级分类作为上级");
        }
        LambdaQueryWrapper<AppMallMenuConfig> check = new LambdaQueryWrapper<>();
        check.eq(AppMallMenuConfig::getLevel,req.getLevel());
        AppMallMenuConfig levelCheck = appMallMenuConfigMapper.selectOne(check);
        if (ObjectUtils.isNotEmpty(levelCheck) && !levelCheck.getId().equals(req.getId())){
            throw new ApiServiceException("已经存在同名的分类");
        }


        AppMallMenuConfig ummc = new AppMallMenuConfig();
        BeanUtils.copyProperties(req,ummc);
        if (ObjectUtils.isNotEmpty(req.getId()) && req.getId() > ConstantsValue.I_0){
            AppMallMenuConfig ammc = appMallMenuConfigMapper.selectById(req.getId());
            if (ObjectUtils.isEmpty(ammc)){
                 throw new ApiServiceException("记录不存在");
            }
            return ApiResponse.create(appMallMenuConfigMapper.updateById(ummc));
        }

        return ApiResponse.create(appMallMenuConfigMapper.insert(ummc));
    }



    @Permission(uri = "/mall/category/push",qx = "/mall/category/saveOrUpdate")
    @Trim
    @PostMapping("/category/push")
    @ApiOperation(value = "商城分类上下架", notes = "商城分类上下架")
    public ApiResponse<Boolean> categoryPush(@RequestBody @Validated MallStoreCategoryPushReq req) {
        AppMallMenuConfig a = appMallMenuConfigMapper.selectById(req.getId());
        if (ObjectUtils.isEmpty(a)){
            throw new ApiServiceException("记录不存在");
        }
        AppMallMenuConfig u = new AppMallMenuConfig();
        u.setId(a.getId());
        if (ConstantsValue.I_1.equals(a.getState())){
            u.setState(ConstantsValue.I_0);
        }else
            u.setState(ConstantsValue.I_1);
        return ApiResponse.create(appMallMenuConfigMapper.updateById(u));
    }






    @Permission(uri = "/mall/store/banner/list",qx = "/mall/bannerList")
    @Trim
    @GetMapping("/store/banner/list")
    @ApiOperation(value = "商城Banner列表", notes = "商城Banner列表")
    public ApiResponse<List<AppMallBanner>> storeBannerList() {
        LambdaQueryWrapper<AppMallBanner> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(AppMallBanner::getSort,AppMallBanner::getCreateTime);
        return ApiResponse.create(appMallBannerMapper.selectList(lqw));
    }


    @Permission(uri = "/mall/store/banner/saveAndUpdate",qx = "/mall/store/banner/saveAndUpdate")
    @Trim
    @PostMapping("/store/banner/saveAndUpdate")
    @ApiOperation(value = "商城banner新增修改", notes = "商城banner新增修改")
    public ApiResponse<Boolean> mallStoreBannerSaveAndUpdate(@RequestBody @Validated MallStoreBannerReq req) {
        return ApiResponse.create(adminMallService.mallStoreBannerSaveAndUpdate(req));
    }

    @Permission(uri = "/mall/store/banner/push",qx = "/mall/store/banner/push")
    @Trim
    @PostMapping("/store/banner/push")
    @ApiOperation(value = "商城banner上下架", notes = "商城banner上下架")
    public ApiResponse<Boolean> mallStoreBannerPush(@RequestBody @Validated MallStoreBannerPushReq req) {
        return ApiResponse.create(adminMallService.mallStoreBannerPush(req));
    }




    @Permission(uri = "/mall/store/audit/list",qx = "/mall/mallUserList")
    @Trim
    @PostMapping("/store/audit/list")
    @ApiOperation(value = "店铺审核-查询", notes = "店铺审核-查询")
    public ApiResponse<PageData<QueryMallStoreAuditListRes>> queryMallStoreAuditList(@RequestBody @Validated QueryMallStoreAuditListReq req) {
        return ApiResponse.create(adminMallService.queryMallStoreAuditList(req));
    }

    @Permission(uri = "/mall/store/auditDro",qx = "/mall/store/auditDro")
    @Trim
    @PostMapping("/store/auditDro")
    @ApiOperation(value = "店铺审核", notes = "店铺审核")
    public ApiResponse<Boolean> mallStoreAuditDro(@RequestBody @Validated MallStoreAuditDroReq req) {
        return ApiResponse.create(adminMallService.mallStoreAuditDro(req));
    }



    @Permission(uri = "/mall/store/audit/bgImageList",qx = "/mall/bgAudit")
    @Trim
    @PostMapping("/store/audit/bgImageList")
    @ApiOperation(value = "店铺审核-背景图片查询", notes = "店铺审核-背景图片查询")
    public ApiResponse<PageData<MallStoreImgAuditRes>> queryMallStoreAuditBgImageList(@RequestBody @Validated QueryMallStoreAuditListReq req) {
        return ApiResponse.create(adminMallService.queryMallStoreAuditBgImageList(req));
    }

    @Permission(uri = "/mall/store/auditBgImageDro",qx = "/mall/store/auditBgImageDro")
    @Trim
    @PostMapping("/store/auditBgImageDro")
    @ApiOperation(value = "店铺背景图片审核", notes = "店铺背景图片审核")
    public ApiResponse<Boolean> mallStoreAuditBgImageDro(@RequestBody @Validated MallStoreAuditImageReq req) {
        return ApiResponse.create(adminMallService.mallStoreAuditBgImageDro(req));
    }


    @Permission(uri = "/mall/store/list",qx = "/mall/auditList")
    @Trim
    @PostMapping("/store/list")
    @ApiOperation(value = "商家列表", notes = "商家列表")
    public ApiResponse<PageData<MallStoreListRes>> mallStoreList(@RequestBody @Validated MallStoreListReq req) {
        return ApiResponse.create(adminMallService.mallStoreList(req));
    }


   // @Permission(uri = "/mall/store/accountList")
    @Trim
    @PostMapping("/store/accountList")
    @ApiOperation(value = "账户流水", notes = "账户流水")
    public ApiResponse<PageData<MallStoreAccountListRes>> storeAccountList(@RequestBody @Validated MallStoreAccountListReq req) {
        return ApiResponse.create(adminMallService.storeAccountList(req));
    }




   // @Permission(uri = "/mall/statisticsData")
    @GetMapping(value = "/statisticsData")
    @ApiOperation(value = "店铺-统计数据")
    public ApiResponse<MallStoreStaticRes> statisticsData() {
        return ApiResponse.create(adminMallService.statisticsData());
    }



    @Permission(uri = "/mall/store/push",qx = "/mall/store/push")
    @Trim
    @PostMapping("/store/push")
    @ApiOperation(value = "店铺上下架", notes = "店铺上下架")
    public ApiResponse<Boolean> storePush(@RequestBody @Validated MallStorePushReq req) {
        AppMallInfo a = appMallInfoMapper.selectById(req.getId());
        if (ObjectUtils.isEmpty(a)){
            throw new ApiServiceException("记录不存在");
        }
        AppMallInfo u = new AppMallInfo();
        u.setId(a.getId());
        if (ConstantsValue.I_1.equals(a.getPublishState())){
            u.setPublishState(ConstantsValue.I_0);
        }else{
            u.setPublishState(ConstantsValue.I_1);
            EcommerceProduct ecommerceProduct = new EcommerceProduct();
            ecommerceProduct.setPublishStatus(1);
            ecommerceProductMapper.update(ecommerceProduct,new QueryWrapper<EcommerceProduct>().eq("mall_id",u.getId()));
        }
        return ApiResponse.create(appMallInfoMapper.updateById(u));
    }



    @Permission(uri = "/mall/store/issueConfig",qx = "/mall/releaseConfig")
    @Trim
    @GetMapping("/store/issueConfig")
    @ApiOperation(value = "消费发行配置", notes = "消费发行配置")
    public ApiResponse<AdminMallIssueConfig> issueConfig() {
        return ApiResponse.create(adminMallIssueConfigMapper.selectById(ConstantsValue.I_1));
    }


    @Permission(uri = "/mall/store/updateIssueConfig",qx = "/mall/store/updateIssueConfig")
    @Trim
    @PostMapping("/store/updateIssueConfig")
    @ApiOperation(value = "消费发行配置修改", notes = "消费发行配置修改")
    public ApiResponse<Boolean> updateIssueConfig(@Validated @RequestBody MallStoreIssueUpdateReq req) {
        AdminMallIssueConfig amic = adminMallIssueConfigMapper.selectById(req.getId());
        if (ObjectUtils.isEmpty(amic)){
            throw new ApiServiceException("记录不存在");
        }
        amic = new AdminMallIssueConfig();
        BeanUtils.copyProperties(req,amic);
        adminMallIssueConfigMapper.updateById(amic);
        return ApiResponse.create(Boolean.TRUE);
    }



    @Permission(uri = "/mall/store/settleInConfig",qx = "/mall/addMallConfig")
    @Trim
    @GetMapping("/store/settleInConfig")
    @ApiOperation(value = "入驻配置", notes = "入驻配置")
    public ApiResponse<AdminMallConfig> settleInConfig() {
        return ApiResponse.create(adminMallConfigMapper.selectById(ConstantsValue.I_1));
    }


    @Permission(uri = "/mall/store/updateMallConfig",qx = "/mall/store/updateMallConfig")
    @Trim
    @PostMapping("/store/updateMallConfig")
    @ApiOperation(value = "入驻配置修改", notes = "入驻配置修改")
    public ApiResponse<Boolean> updateMallConfig(@Validated @RequestBody MallStoreConfigUpdateReq req) {
        AdminMallConfig amic = adminMallConfigMapper.selectById(req.getId());
        if (ObjectUtils.isEmpty(amic)){
            throw new ApiServiceException("记录不存在");
        }
        amic = new AdminMallConfig();
        amic.setId(req.getId());
        amic.setNeedIssue(req.getNeedIssue());
        amic.setIssueNumber(req.getIssueNumber());
        adminMallConfigMapper.updateById(amic);
        return ApiResponse.create(Boolean.TRUE);
    }

    @Permission(uri = "/mall/store/addCard",qx = "/mall/store/addCard")
    @Trim
    @GetMapping("/store/addCard")
    @ApiOperation(value = "添加卡劵", notes = "添加卡劵")
    public ApiResponse addCard(@RequestParam Integer userId,@RequestParam Integer quantity,@RequestParam Integer type) {
        AdminCardConfig adminCardConfig = adminCardConfigMapper.selectById(1);
        if(type.equals(1)){
        for (int i = 0; i < quantity; i++) {
            AppMallCardInfo appMallCardInfo = new AppMallCardInfo();
            appMallCardInfo.setUserId(userId);
            appMallCardInfo.setState(1);
            appMallCardInfo.setDeadline(adminCardConfig.getShelfLife());
            appMallCardInfo.setAcceleration(adminCardConfig.getAccelerationTime());
            appMallCardInfo.setType(1);
            appMallCardInfo.setCreateTime(LocalDateTime.now());
            appMallCardInfo.setExpiredTime(appMallCardInfo.getCreateTime().plusDays(appMallCardInfo.getDeadline()));
            appMallCardInfo.insert();
        }
        }else{
                QueryWrapper<AppMallCardInfo> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("user_id", userId).eq("state",1).last("limit "+quantity);
                appMallCardInfoMapper.delete(queryWrapper1);
            }


        return ApiResponse.create();
    }

    @Permission(uri = "/mall/store/getCardConfig",qx = "/configuration/cards")
    @Trim
    @GetMapping("/store/getCardConfig")
    @ApiOperation(value = "查询卡劵配置", notes = "查询卡劵配置")
    public ApiResponse<AdminCardConfig> getCardConfig() {
        AdminCardConfig adminCardConfig = adminCardConfigMapper.selectById(1);
        return ApiResponse.create(adminCardConfig);
    }

    @Permission(uri = "/mall/store/updateCardConfig",qx = "/mall/store/addCard")
    @Trim
    @PostMapping("/store/updateCardConfig")
    @ApiOperation(value = "修改卡劵配置", notes = "修改卡劵配置")
    public ApiResponse<Boolean> updateCardConfig(@RequestBody AdminCardConfig adminCardConfig) {
        return ApiResponse.create(adminCardConfig.insertOrUpdate());
    }





    @Permission(uri = "/mall/store/levelConfig",qx = "/configuration/mallLevel")
    @PostMapping("/store/levelConfig")
    @ApiOperation(value = "查询升星配置", notes = "查询升星配置")
    public ApiResponse<MallLevelConfigRes> levelConfig() {
        LambdaQueryWrapper<AdminMallLevelConfig> lqwAmlc = new LambdaQueryWrapper<>();
        lqwAmlc.eq(AdminMallLevelConfig::getState,ConstantsValue.I_0).orderByAsc(AdminMallLevelConfig::getLevel);
        List<AdminMallLevelConfig> configs = adminMallLevelConfigMapper.selectList(lqwAmlc);
        AdminMallConfig config = adminMallConfigMapper.selectById(ConstantsValue.I_1);
        MallLevelConfigRes res = new MallLevelConfigRes();
        res.setConfigs(configs);
        res.setPersonalInitLevel(config.getPersonalInitLevel());
        res.setPersonalMaxLevel(config.getPersonalMaxLevel());
        res.setBusinessInitLevel(config.getBusinessInitLevel());
        res.setBusinessMaxLevel(config.getBusinessMaxLevel());
        return ApiResponse.create();
    }


    @Permission(uri = "/mall/store/updateLevelConfig",qx = "/configuration/updateLevelConfig")
    @PostMapping("/store/updateLevelConfig")
    @ApiOperation(value = "修改升星配置", notes = "修改升星配置")
    @Transactional
    public ApiResponse levelConfig(@Validated @RequestBody MallLevelConfigRes req) {
        AdminMallConfig c = new AdminMallConfig();
        c.setId(req.getId());
        c.setPersonalInitLevel(req.getPersonalInitLevel());
        c.setPersonalMaxLevel(req.getPersonalMaxLevel());
        c.setBusinessInitLevel(req.getBusinessInitLevel());
        c.setBusinessMaxLevel(req.getBusinessMaxLevel());
        if (CollectionUtils.isNotEmpty(req.getConfigs())){
            req.getConfigs().forEach(x->{
                AdminMallLevelConfig ac = new AdminMallLevelConfig();
                ac.setId(x.getId());
                ac.setScore(x.getScore());
                adminMallLevelConfigMapper.updateById(ac);
            });
        }
        return ApiResponse.create();
    }

    @Permission(uri = "/mall/getMallIssueSumAmount",qx = "/mall/getMallIssueSumAmount")
    @GetMapping(value = "/getMallIssueSumAmount")
    @ApiOperation(value = "getMallIssueSumAmount", notes = "商城发发行流通统计")
    public ApiResponse getMallIssueSumAmount() {
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("one",appUserAssetsHistoryMapper.getAmountByToday());
        map.put("two",appUserAssetsHistoryMapper.getAmountByYesterday());
        map.put("three",appUserAssetsHistoryMapper.getAmountByMonth());
        map.put("four",appUserAssetsHistoryMapper.getSumAmountBy());
        return ApiResponse.create(map);
    }
    @Permission(uri = "/mall/getMallIssue",qx = "/mall/getMallIssue")
    @PostMapping(value = "/getMallIssue")
    @ApiOperation(value = "getMallIssue", notes = "查询流通记录")
    public ApiResponse<Page<MallIssuDetailRes>> getMallIssue(@Validated @RequestBody MallIssuDetailReq req){
        Page<MallIssuDetailRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        return ApiResponse.create(PageDataUtil.getPageData(appUserAssetsHistoryMapper.getMallIssueDetail(page,req)));
    }





    @Permission(uri = "/mall/management/category",qx = "/mall/management/category")
    @PostMapping (value = "/management/category")
    @ApiOperation(value = "/management/category", notes = "商城管理新增分类")
    public ApiResponse<Integer> mallManagementCategoryAdd(@Validated @RequestBody EcommerceProductCategory req){
        if (ObjectUtils.isNotEmpty(req.getParentId()) && req.getParentId() > ConstantsValue.I_0){
            EcommerceProductCategory parent = ecommerceProductCategoryMapper.selectById(req.getParentId());
            if (ObjectUtils.isEmpty(parent))
                throw new ApiServiceException("上级分类不存在");
            if (!parent.getDeleteStatus().equals(ConstantsValue.I_1))
                throw new ApiServiceException("上级不可用");
            if (ObjectUtils.isNotEmpty(parent.getParentId()) && !parent.getParentId().equals(ConstantsValue.I_0))
                throw new ApiServiceException("只允许存在二级分类，请选择一级分类作为上级");
        }
        LambdaQueryWrapper<EcommerceProductCategory> check = new LambdaQueryWrapper<>();
        check.eq(EcommerceProductCategory::getName, req.getName());
        check.eq(EcommerceProductCategory::getLevel,req.getLevel());
        EcommerceProductCategory levelCheck = ecommerceProductCategoryMapper.selectOne(check);
        if (ObjectUtils.isNotEmpty(levelCheck) && !levelCheck.getId().equals(req.getId())){
            throw new ApiServiceException("已经存在同名的分类");
        }


        EcommerceProductCategory ummc = new EcommerceProductCategory();
        BeanUtils.copyProperties(req,ummc);
        if (ObjectUtils.isNotEmpty(req.getId()) && req.getId() > ConstantsValue.I_0){
            MallCategory ammc = categoryMapper.selectById(req.getId());
            if (ObjectUtils.isEmpty(ammc)){
                throw new ApiServiceException("记录不存在");
            }
            return ApiResponse.create(ecommerceProductCategoryMapper.updateById(ummc));
        }

        return ApiResponse.create(ecommerceProductCategoryMapper.insert(ummc));
    }


    @Permission(uri = "/management/category/on_off",qx = "/mall/management/category/del")
    @PostMapping(value = "/management/category/on_off")
    @ApiOperation(value = "/management/category/on_off", notes = "商城管理分类上下架")
    public ApiResponse<Page<MallCategoriesRes>> mallManagementCategoryDel(@Validated @RequestBody MallStoreCategoryPushReq req){
        EcommerceProductCategory a = ecommerceProductCategoryMapper.selectById(req.getId());
        if (ObjectUtils.isEmpty(a)){
            throw new ApiServiceException("记录不存在");
        }
        EcommerceProductCategory u = new EcommerceProductCategory();
        u.setId(a.getId());
        if (ConstantsValue.I_1.equals(a.getShowStatus())){
            u.setShowStatus(ConstantsValue.I_0);
        }else
            u.setShowStatus(ConstantsValue.I_1);
        return ApiResponse.create(ecommerceProductCategoryMapper.updateById(u));
    }


//    @Permission(uri = "/mall/management/category/update",qx = "/mall/management/category/update")
//    @PostMapping(value = "/getMallIssue")
//    @ApiOperation(value = "/management/category/update", notes = "商城管理修改分类")
//    public ApiResponse<Page<MallIssuDetailRes>> mallManagementCategoryUpdate(@Validated @RequestBody MallIssuDetailReq req){
//        Page<MallIssuDetailRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
//        return ApiResponse.create(PageDataUtil.getPageData(appUserAssetsHistoryMapper.getMallIssueDetail(page,req)));
//    }


    @Permission(uri = "/mall/management/categories",qx = "/mall/management/categories")
    @PostMapping(value = "/management/categories")
    @ApiOperation(value = "/management/categories", notes = "商城管理查询分类")
    public ApiResponse<Page<EcommerceProductCategory>> mallManagementCategories(@Validated @RequestBody MallStoreCategoryListReq req){
        Page<EcommerceProductCategory> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
//        Page res = categoryMapper.getCategoryRecord(page,req);

        LambdaQueryWrapper<EcommerceProductCategory> queryWrapper = new LambdaQueryWrapper<>();

        ecommerceProductCategoryMapper.selectPage(page, queryWrapper);


        return ApiResponse.create(PageDataUtil.getPageData(page));
    }


    @Permission(uri = "/mall/issue/amount",qx = "/mall/issue/amount")
    @Trim
    @GetMapping("/issue/amount")
    @ApiOperation(value = "发型记录-数据统计", notes = "发型记录-数据统计")
    public ApiResponse mallIssueAmout() {
        Map<String,BigDecimal> map = new HashMap<>();
        map.put("one",issueRecordsMapper.getMallIssueAmout(0,"1"));
        map.put("two",issueRecordsMapper.getMallIssueAmout(1,"1"));
        map.put("three",map.get("one").add(map.get("two")));
        map.put("four",issueRecordsMapper.getMallIssueAmout(0,"2"));
        map.put("five",issueRecordsMapper.getMallIssueAmout(1,"2"));
        map.put("six",map.get("four").add(map.get("five")));
        map.put("seven",issueRecordsMapper.getMallIssueAmout(0,"3"));
        map.put("eight", issueRecordsMapper.getMallIssueAmout(1,"3"));
        map.put("nine",map.get("seven").add(map.get("eight")));
        map.put("ten",issueRecordsMapper.getMallIssueAmout(0,null));
        map.put("eleven",issueRecordsMapper.getMallIssueAmout(1,null));
        map.put("twelve",map.get("eleven").add(map.get("eleven")));
        map.put("Thirteen",BigDecimal.ZERO);
        return ApiResponse.create(map);
    }


    @Trim
    @PostMapping ("/issue/records")
    @Permission(uri = "/mall/issue/records",qx = "/mall/issue/records")
    @ApiOperation(value = "发行记录", notes = "发行记录")
    public ApiResponse<Page<MallIssueRelordRes>> getMallIssueDetail(@RequestBody MallIssueRelordReq req) {
        Page<MallIssueRelordRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<MallIssueRelordRes> res = issueRecordsMapper.getMallIssueDetail(page,req);
        return ApiResponse.create( PageDataUtil.getPageData(res, MallIssueRelordRes.class));
    }


    @Permission(uri = "/mall/circulation/histories/amount",qx = "/mall/assets/amount")
    @GetMapping(value = "/circulation/histories/amount")
    @ApiOperation(value = "/circulation/histories/amount", notes = "商城发发行流通统计")
    public ApiResponse userAssetsAmount() {
        Map<String, BigDecimal> map = new HashMap<>();
        map.put("one",circulationHistoriesMapper.getAmountByToday());
        map.put("two",circulationHistoriesMapper.getAmountByYesterday());
        map.put("three",circulationHistoriesMapper.getAmountByMonth());
        map.put("four",circulationHistoriesMapper.getSumAmountBy());
        return ApiResponse.create(map);
    }

    @Permission(uri = "/mall/circulation/histories",qx = "/mall/assets/histories")
    @PostMapping(value = "/circulation/histories")
    @ApiOperation(value = "/circulation/histories", notes = "查询流通记录")
    public ApiResponse<Page<MallIssuDetailRes>> userAssetsHistries(@Validated @RequestBody MallIssuDetailReq req){

        Page<MallIssuDetailRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        return ApiResponse.create(PageDataUtil.getPageData(circulationHistoriesMapper.getMallIssueDetail(page,req)));
    }





    @Permission(uri = "/mall/logOff/applyList",qx = "/mall/logOff/applyList")
    @PostMapping(value = "/logOff/applyList")
    @ApiOperation(value = "/logOff/applyList", notes = "退店申请列表")
    public ApiResponse<Page<MallLogOffApplyRes>> logOffApplyList(@Validated @RequestBody MallApplyListReq req){
        Page<MallLogOffApplyRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Token token = tokenMapper.selectById(LetokenTokenTypeEnum.CAP.getId());
        if (ObjectUtils.isEmpty(token)){
            throw new ApiServiceException("币种账户记录不存在");
        }
        if (ObjectUtils.isNotEmpty(req.getLeId())){
            if (req.getLeId().startsWith("le")){
                req.setLeId(req.getLeId().substring(2));
            }
            try {
                req.setUserId(Integer.valueOf(req.getLeId()));
            } catch (Exception e){
                req.setUserId(null);
            }
        }

        Page<MallLogOffApplyRes> f =  appMallAuditMapper.logOffApplyList(page,req);
        if (CollectionUtils.isNotEmpty(f.getRecords())){
            for (MallLogOffApplyRes record : f.getRecords()) {
                record.setBondStr(String.format("%su≈%scap",record.getUsdtValue(),record.getUsdtValue().divide(new BigDecimal(token.getUsdPrice()),2)));
            }
        }

        return ApiResponse.create(f);
    }

    @Permission(uri = "/mall/logOff/audit",qx = "/mall/logOff/audit")
    @PostMapping(value = "/logOff/audit")
    @ApiOperation(value = "/logOff/audit", notes = "退店审核")
    public ApiResponse logOffAudit(@Validated @RequestBody MallLogOffReq req){
        adminMallService.logOffAudit(req);
        return ApiResponse.create();
    }


}
